# packages needed
p_needed <- c("tidyverse", "devtools", "rvest", "xml2",
              "cmdstanr", "bayesplot", "posterior", "abind",
              "Rvoteview", "readxl", "loo", "ggrepel", "ellipse"
)

# install packages
packages <- rownames(installed.packages())
p_to_install <- p_needed[!(p_needed %in% packages)]
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}

if (!("cmdstanr" %in% packages)){
  devtools::install_github("stan-dev/cmdstanr")
  cmdstanr::install_cmdstan(
    dir = NULL,
    cores = getOption("mc.cores", 4),
    quiet = FALSE,
    overwrite = TRUE,
    timeout = 1200,
    version = NULL,
    release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.27.0/cmdstan-2.27.0.tar.gz",
    cpp_options = list(),
    check_toolchain = TRUE
  )
}

cmdstanr::set_cmdstan_path("/home/rstudio/.cmdstan/cmdstan-2.27.0")

if (!("Rvoteview" %in% packages)){
  devtools::install_github("voteview/Rvoteview")
}

lapply(p_needed, require, character.only = TRUE)

rm(list = ls());gc()

sessionInfo()
# R version 4.1.1 (2021-08-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 20.04.3 LTS
# 
# Matrix products: default
# BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
# LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
# 
# locale:
#   [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8    LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
# [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] ggrepel_0.9.1   readxl_1.3.1    Rvoteview_0.1   abind_1.4-5     posterior_1.1.0 bayesplot_1.8.1 cmdstanr_0.4.0  xml2_1.3.2      rvest_1.0.2     devtools_2.4.2  usethis_2.1.0   forcats_0.5.1   stringr_1.4.0  
# [14] purrr_0.3.4     readr_2.0.2     tibble_3.1.5    ggplot2_3.3.5   tidyverse_1.3.1 loo_2.4.1       tidyr_1.1.4     dplyr_1.0.7    
# 
# loaded via a namespace (and not attached):
#   [1] matrixStats_0.61.0   fs_1.5.0             lubridate_1.8.0      httr_1.4.2           rprojroot_2.0.2      rstan_2.21.2         tensorA_0.36.2       tools_4.1.1          backports_1.2.1      utf8_1.2.2          
# [11] R6_2.5.1             DBI_1.1.1            colorspace_2.0-2     withr_2.4.2          tidyselect_1.1.1     gridExtra_2.3        prettyunits_1.1.1    processx_3.5.2       curl_4.3.2           compiler_4.1.1      
# [21] cli_3.0.1            desc_1.4.0           scales_1.1.1         checkmate_2.0.0      ggridges_0.5.3       callr_3.7.0          StanHeaders_2.21.0-7 pscl_1.5.5           pkgconfig_2.0.3      sessioninfo_1.1.1   
# [31] dbplyr_2.1.1         fastmap_1.1.0        rlang_0.4.11         rstudioapi_0.13      generics_0.1.0       farver_2.1.0         jsonlite_1.7.2       distributional_0.2.2 inline_0.3.19        magrittr_2.0.1      
# [41] Rcpp_1.0.7           munsell_0.5.0        fansi_0.5.0          lifecycle_1.0.1      stringi_1.7.5        MASS_7.3-54          pkgbuild_1.2.0       plyr_1.8.6           grid_4.1.1           parallel_4.1.1      
# [51] crayon_1.4.1         haven_2.4.3          hms_1.1.1            knitr_1.36           ps_1.6.0             pillar_1.6.3         codetools_0.2-18     stats4_4.1.1         pkgload_1.2.3        fastmatch_1.1-3     
# [61] reprex_2.0.1         glue_1.4.2           V8_3.4.2             remotes_2.4.1        RcppParallel_5.1.4   modelr_0.1.8         vctrs_0.3.8          tzdb_0.1.2           testthat_3.1.0       cellranger_1.1.0    
# [71] gtable_0.3.0         assertthat_0.2.1     cachem_1.0.6         xfun_0.26            broom_0.7.9          rstiefel_1.0.1       memoise_2.0.0        ellipsis_0.3.2      